package com.xteam.xnetworkdisk.sys.service.impl;

import com.xteam.xnetworkdisk.sys.entity.ByteArrayMultipartFile;
import com.xteam.xnetworkdisk.sys.service.FileService;
import com.xteam.xnetworkdisk.vo.UploadChunkMessageVO;
import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.web.multipart.MultipartFile;
//@Service
@Slf4j
public class ChunkUploadConsumerService {
//    @Autowired
    private FileService fileService;

//    @RabbitListener(queues = "upload.queue")
    public void handleChunkUpload(UploadChunkMessageVO message) {

        log.info("handle chunk upload message: {}", message);
        // 从消息中提取分片上传的参数
        byte[] chunkFileBytes = message.getChunkFile();
        String md5 = message.getMd5();
        Integer folderId = message.getFolderId();
        Integer chunkIndex = message.getChunkIndex();
        Integer chunks = message.getChunks();
        String fileName = message.getFileName();
        Long fileSize = message.getFileSize();

        // 创建 MultipartFile 实例
        MultipartFile chunkFile = new ByteArrayMultipartFile(
                chunkFileBytes,
                "chunkFile", // Name for the file
                fileName, // Original filename
                "application/octet-stream" // Content type
        );

        // 调用分片上传服务
        // 注意：这里的 fileService.uploadChunk 方法可以同步执行
//        fileService.uploadChunk(chunkFile, md5, folderId, chunkIndex, chunks, fileName, fileSize, userid);
    }
}
